#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/7/14
# @Author  : LiJun
# @Email   : lijun@tianyancha.com
# @Version : 1.0.0
import json

import mitmproxy.http

from mitmproxy import ctx, http
from conf import config as cf

class MapLocalTest:

    def response(self, flow: mitmproxy.http.HTTPFlow):
        """
            The full HTTP response has been read.
        """
        batch_url = "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t="
        realtime_url = "https://stock.xueqiu.com/v5/stock/realtime/quotec.json?_t="
        if batch_url in flow.request.pretty_url:
            ctx.log.info(f"++++++++++++\n {flow.response.text}")
            with open(f"{cf.LOG_DIR}/batch_maplocal.json", encoding="utf-8") as f:
                flow.response = http.HTTPResponse.make(
                    200,
                    f.read()
                )
        if realtime_url in flow.request.pretty_url:
            ctx.log.info(f"==========\n {flow.response.text}")



addons = [
    MapLocalTest()
]


if __name__ == '__main__':
    from mitmproxy.tools.main import mitmdump

    mitmdump(['-p', '8080', '-s', __file__])
